#include <stdlib.h>
#include <string.h>
#include <cjson/cJSON.h>
#include "json_parser.h"

song_info_t *parse_songinfo_json(char *json_data)
{
    cJSON *root = cJSON_Parse(json_data);
    cJSON *node = NULL;
    if (!root)
    {
        //error
        exit(-1);
    }
    node = cJSON_GetObjectItemCaseSensitive(root, "songs");
    node = cJSON_GetArrayItem(node, 0);
    if (!node)
    {
        //empty
        return NULL;
    }
    song_info_t *res = malloc(sizeof(song_info_t));

    //now get info
    cJSON *needle = NULL;

    //get song name
    needle = cJSON_GetObjectItemCaseSensitive(node, "name");
    if (!needle)
    {
        //empty
        res->name = NULL;
    }
    else
    {
        res->name = malloc(strlen(needle->valuestring) + 1);
        strcpy(res->name, needle->valuestring);
    }

    //get song transname, get the first one only if there are any.
    needle = cJSON_GetObjectItemCaseSensitive(node, "transNames");
    needle = cJSON_GetArrayItem(needle, 0);
    if (!needle)
    {
        //empty
        res->transname = NULL;
    }
    else
    {
        res->transname = malloc(strlen(needle->valuestring) + 1);
        strcpy(res->transname, needle->valuestring);
    }

    //get song alias, get the first one only if there are any.
    needle = cJSON_GetObjectItemCaseSensitive(node, "alias");
    needle = cJSON_GetArrayItem(needle, 0);
    if (!needle)
    {
        //empty
        res->alias = NULL;
    }
    else
    {
        res->alias = malloc(strlen(needle->valuestring) + 1);
        strcpy(res->alias, needle->valuestring);
    }

    //get song index
    needle = cJSON_GetObjectItemCaseSensitive(node, "no");
    if (!needle)
    {
        //empty
        res->index = 0;
    }
    else
    {
        res->index = needle->valueint;
    }

    //get song disc
    needle = cJSON_GetObjectItemCaseSensitive(node, "disc");
    if (!needle)
    {
        //empty
        res->disc = NULL;
    }
    else
    {
        res->disc = malloc(strlen(needle->valuestring) + 1);
        strcpy(res->disc, needle->valuestring);
    }

    //get artists
    needle = cJSON_GetObjectItemCaseSensitive(node, "artists");
    int arraysize = cJSON_GetArraySize(needle);
    res->artist = malloc(1);
    res->artist[0] = 0;
    for (int i = 0; i < arraysize; ++i)
    {
        cJSON *picker = cJSON_GetArrayItem(needle, i);
        picker = cJSON_GetObjectItemCaseSensitive(picker, "name");
        char *buff = malloc(strlen(res->artist) + strlen(picker->valuestring) + 3);
        strcpy(buff, res->artist);
        strcat(buff, picker->valuestring);
        if (i != arraysize - 1)
            strcat(buff, "; ");
        free(res->artist);
        res->artist = buff;
        buff = NULL;
    }

    //get album name
    needle = cJSON_GetObjectItemCaseSensitive(node, "album");
    needle = cJSON_GetObjectItemCaseSensitive(needle, "name");
    if (!needle)
    {
        //empty
        res->album = NULL;
    }
    else
    {
        res->album = malloc(strlen(needle->valuestring) + 1);
        strcpy(res->album, needle->valuestring);
    }

    //get album cover url
    needle = cJSON_GetObjectItemCaseSensitive(node, "album");
    needle = cJSON_GetObjectItemCaseSensitive(needle, "picUrl");
    if (!needle)
    {
        //empty
        res->album_picurl = NULL;
    }
    else
    {
        res->album_picurl = malloc(strlen(needle->valuestring) + 1);
        strcpy(res->album_picurl, needle->valuestring);
    }

    //done
    return res;
}
